//
//  XYReadDetailsController.swift
//  XYReadBook
//
//  Created by fox on 2017/10/7.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit


/// 阅读控制器 View的点击事件都由这个控制器处理
class XYReadDetailsController: UIViewController {

    var index: Int
    var frameRef: CTFrame
    var height: CGFloat
    var model:XYReadChapterModel
    var totalBookCoin:Int = 0
    var oldPoint:CGPoint?
    var readModel:XYReadModel
    var chapterTitle = UILabel()

    var buttomView = XWJReadStateView()

    init(index: Int, frameRef: CTFrame, height: CGFloat,model:XYReadChapterModel,readModel:XYReadModel) {
        self.index = index
        self.frameRef = frameRef
        self.height = height
        self.model = model
        self.readModel = readModel
        super.init(nibName: nil, bundle: nil)

    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func loadView() {
         view = XYReadDetaillsView(frameRef: frameRef, txtTeight: height)
    }


    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        chapterTitle.frame = CGRect.init(x: 0, y: 10, width: ScreenWidth, height: 20)
        buttomView.frame = CGRect.init(x: 0, y: ScreenHeight - 30, width: ScreenWidth, height: 30)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = DZMReadConfigure.shared().readColor()
        view.addSubview(chapterTitle)
        chapterTitle.textAlignment = .center
        chapterTitle.textColor = DZMReadTextColors[DZMReadConfigure.shared().colorIndex]
        chapterTitle.text = model.titleString

        // 设置电池电量,设置百分比
        view.addSubview(buttomView)
        buttomView.pageString = String.init(format: "%d/%d页",index + 1,model.pageRanges.count)

        let pageIndexKey = model.bookID + "pageIndex"
        UserDefaults.standard.set(index, forKey: pageIndexKey)



        let chapterIndex = readModel.bookID + "index"
        UserDefaults.standard.set(  readModel.index, forKey: chapterIndex)
    }

    deinit {
        XYLog(messsage: "readDetaills挂了")

    }
}


